home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxpdash.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.4 KB  |  184 lines

  1. /* Copyright (C) 1995, 1996, 1997, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxpdash.c,v 1.2 2000/09/19 19:00:40 lpd Exp $ */
  20. /* Dash expansion for paths */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gsmatrix.h"        /* for gscoord.h */
  24. #include "gscoord.h"
  25. #include "gxfixed.h"
  26. #include "gsline.h"
  27. #include "gzline.h"
  28. #include "gzpath.h"
  29.  
  30. /* Expand a dashed path into explicit segments. */
  31. /* The path contains no curves. */
  32. private int subpath_expand_dashes(P4(const subpath *, gx_path *,
  33.                      const gs_imager_state *,
  34.                      const gx_dash_params *));
  35. int
  36. gx_path_add_dash_expansion(const gx_path * ppath_old, gx_path * ppath,
  37.                const gs_imager_state * pis)
  38. {
  39.     const subpath *psub;
  40.     const gx_dash_params *dash = &gs_currentlineparams(pis)->dash;
  41.     int code = 0;
  42.  
  43.     if (dash->pattern_size == 0)
  44.     return gx_path_copy(ppath_old, ppath);
  45.     for (psub = ppath_old->first_subpath; psub != 0 && code >= 0;
  46.      psub = (const subpath *)psub->last->next
  47.     )
  48.     code = subpath_expand_dashes(psub, ppath, pis, dash);
  49.     return code;
  50. }
  51.  
  52. private int
  53. subpath_expand_dashes(const subpath * psub, gx_path * ppath,
  54.            const gs_imager_state * pis, const gx_dash_params * dash)
  55. {
  56.     const float *pattern = dash->pattern;
  57.     int count, index;
  58.     bool ink_on;
  59.     double elt_length;
  60.     fixed x0 = psub->pt.x, y0 = psub->pt.y;
  61.     fixed x, y;
  62.     const segment *pseg;
  63.     int wrap = (dash->init_ink_on && psub->is_closed ? -1 : 0);
  64.     int drawing = wrap;
  65.     segment_notes notes = ~sn_not_first;
  66.     int code;
  67.  
  68.     if ((code = gx_path_add_point(ppath, x0, y0)) < 0)
  69.     return code;
  70.     /*
  71.      * To do the right thing at the beginning of a closed path, we have
  72.      * to skip any initial line, and then redo it at the end of the
  73.      * path.  Drawing = -1 while skipping, 0 while drawing normally, and
  74.      * 1 on the second round.  Note that drawing != 0 implies ink_on.
  75.      */
  76.   top:count = dash->pattern_size;
  77.     ink_on = dash->init_ink_on;
  78.     index = dash->init_index;
  79.     elt_length = dash->init_dist_left;
  80.     x = x0, y = y0;
  81.     pseg = (const segment *)psub;
  82.     while ((pseg = pseg->next) != 0 && pseg->type != s_start) {
  83.     fixed sx = pseg->pt.x, sy = pseg->pt.y;
  84.     fixed udx = sx - x, udy = sy - y;
  85.     double length, dx, dy;
  86.     double scale = 1;
  87.     double left;
  88.  
  89.     if (!(udx | udy))    /* degenerate */
  90.         dx = 0, dy = 0, length = 0;
  91.     else {
  92.         gs_point d;
  93.  
  94.         dx = udx, dy = udy;    /* scaled as fixed */
  95.         gs_imager_idtransform(pis, dx, dy, &d);
  96.         length = hypot(d.x, d.y) * (1.0 / fixed_1);
  97.         if (gs_imager_currentdashadapt(pis)) {
  98.         double reps = length / dash->pattern_length;
  99.  
  100.         scale = reps / ceil(reps);
  101.         /* Ensure we're starting at the start of a */
  102.         /* repetition.  (This shouldn't be necessary, */
  103.         /* but it is.) */
  104.         count = dash->pattern_size;
  105.         ink_on = dash->init_ink_on;
  106.         index = dash->init_index;
  107.         elt_length = dash->init_dist_left * scale;
  108.         }
  109.     }
  110.     left = length;
  111.     while (left > elt_length) {    /* We are using up the line segment. */
  112.         double fraction = elt_length / length;
  113.         fixed nx = x + (fixed) (dx * fraction);
  114.         fixed ny = y + (fixed) (dy * fraction);
  115.  
  116.         if (ink_on) {
  117.         if (drawing >= 0)
  118.             code = gx_path_add_line_notes(ppath, nx, ny,
  119.                           notes & pseg->notes);
  120.         notes |= sn_not_first;
  121.         } else {
  122.         if (drawing > 0)    /* done */
  123.             return 0;
  124.         code = gx_path_add_point(ppath, nx, ny);
  125.         notes &= ~sn_not_first;
  126.         drawing = 0;
  127.         }
  128.         if (code < 0)
  129.         return code;
  130.         left -= elt_length;
  131.         ink_on = !ink_on;
  132.         if (++index == count)
  133.         index = 0;
  134.         elt_length = pattern[index] * scale;
  135.         x = nx, y = ny;
  136.     }
  137.     elt_length -= left;
  138.     /* Handle the last dash of a segment. */
  139.       on:if (ink_on) {
  140.         if (drawing >= 0) {
  141.         code =
  142.             (pseg->type == s_line_close && drawing > 0 ?
  143.              gx_path_close_subpath_notes(ppath,
  144.                          notes & pseg->notes) :
  145.              gx_path_add_line_notes(ppath, sx, sy,
  146.                         notes & pseg->notes));
  147.         notes |= sn_not_first;
  148.         }
  149.     } else {
  150.         code = gx_path_add_point(ppath, sx, sy);
  151.         notes &= ~sn_not_first;
  152.         if (elt_length < fixed2float(fixed_epsilon) &&
  153.         (pseg->next == 0 || pseg->next->type == s_start)
  154.         ) {        /*
  155.                  * Ink is off, but we're within epsilon of the end
  156.                  * of the dash element, and at the end of the
  157.                  * subpath.  "Stretch" a little so we get a dot.
  158.                  */
  159.         if (code < 0)
  160.             return code;
  161.         elt_length = 0;
  162.         ink_on = true;
  163.         if (++index == count)
  164.             index = 0;
  165.         elt_length = pattern[index] * scale;
  166.         goto on;
  167.         }
  168.         if (drawing > 0)    /* done */
  169.         return code;
  170.         drawing = 0;
  171.     }
  172.     if (code < 0)
  173.         return code;
  174.     x = sx, y = sy;
  175.     }
  176.     /* Check for wraparound. */
  177.     if (wrap && drawing <= 0) {    /* We skipped some initial lines. */
  178.     /* Go back and do them now. */
  179.     drawing = 1;
  180.     goto top;
  181.     }
  182.     return 0;
  183. }
  184.